package com.xuan.yupao.interceptor;

import com.xuan.yupao.common.ErrorCode;
import com.xuan.yupao.constant.UserConstant;
import com.xuan.yupao.exception.BusinessException;
import com.xuan.yupao.model.domain.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User) request.getAttribute(UserConstant.USER_SESSION);
        if (user == null || user.getId() < 0) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        return true;
    }
}
